// @ts-ignore
/* eslint-disable */
import {request} from 'umi';
import {CoursewareBody, CoursewareListItem} from './data';


export async function dataList(params: {
  // query
  /** 当前的页码 */
  current?: number;
  /** 页面的容量 */
  pageSize?: number;
  orderByColumn?: number;
  isAsc?: number;
}) {
  return request<{
    data: CoursewareListItem[];
    /** 列表的内容总数 */
    total?: number;
    success?: boolean;
  }>('/v2/enterprise/train/courseware/list', {
    method: 'GET',
    params: {
      ...params,
      pageNum: params.current,
      orderByColumn: params.orderByColumn ?? 'createTime',
      isAsc: params.isAsc ?? 'desc',
    },
  });
}

export async function loadData(param: CoursewareBody): Promise<{ data: CoursewareBody }> {
  return request(`/v2/enterprise/train/courseware/${param.coursewareId}`, {method: 'GET'});
}

export async function loadDataByHash(hash: String): Promise<{ data: CoursewareBody }> {
  return request(`/v2/enterprise/train/courseware/hash/${hash}`, {method: 'GET'});
}

export async function saveHash(param: any): Promise<{ data: CoursewareBody }> {
  return request('/v2/enterprise/train/courseware/hash-info', {method: 'POST', data: param});
}

export async function addData(param: CoursewareBody): Promise<{ data: CoursewareListItem }> {
  return request('/v2/enterprise/train/courseware', {method: 'POST', data: param});
}

export async function updateData(param: CoursewareBody): Promise<{ data: number }> {
  return request('/v2/enterprise/train/courseware', {method: 'PUT', data: param});
}

export async function uploadAuth(param: any): Promise<{ data: any }> {
  return request('/v2/enterprise/train/courseware/upload-auth', {method: 'POST', data: param});
}

export async function refreshUploadAuth(param: any): Promise<{ data: any }> {
  return request('/v2/enterprise/train/courseware/refresh-upload-auth', {method: 'POST', data: param});
}

export async function uploadPdf(file: File): Promise<{ data: any }> {
  let formData = new FormData();
  formData.append('file', file);
  return request('/v2/enterprise/train/courseware/upload', {
    method: 'POST',
    body: formData,
    onReqProgress: function (ev: ProgressEvent) {
      console.log(ev)
    }
  });
}

export async function removeData(data: number[], options?: { [key: string]: any }) {
  return request<Record<string, any>>(`/v2/enterprise/train/courseware/` + data.join(), {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function removeQuestionData(dataId: number, data: number[], options?: { [key: string]: any }) {
  return request<Record<string, any>>(`/v2/enterprise/train/courseware/${dataId}/` + data.join(), {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function loadSelectTag() {
  return request(`/v2/enterprise/train/courseware/tags`, {method: "GET"});
}

export async function download() {
  request(`/v2/enterprise/train/courseware/question/template`, {
    method: 'POST',
    // 必须加responseType: 'blob',
    responseType: 'blob',
  }).then((res) => {
    const objectURL = URL.createObjectURL(res);
    let btn = document.createElement('a');
    btn.download = '试题模板.xlsx'; //文件类型
    btn.href = objectURL;
    btn.click();
    URL.revokeObjectURL(objectURL);
    // @ts-ignore
    btn = null;
  });
}


export async function questionList(dataId: number) {
  return request(`/v2/enterprise/train/courseware/${dataId}/list`, {
    method: 'GET',
  });
}


